﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace NinjaToolbox.SubComponentModule.Example
{
    public class SE2_Move : SubComponent<SE2_Move_Model>
    {
        Animator mCacheAnimator;
        Transform mCacheTransform;
        SE2_FSM mFSM;


        protected override void SubComponent_OnEnable()
        {
            base.SubComponent_OnEnable();

            mCacheTransform = (mContext.Owner as MonoBehaviour).transform;
            mCacheAnimator = (mContext.Owner as MonoBehaviour).GetComponentInChildren<Animator>();
            mFSM = mContext.Container.GetSubComponent<SE2_FSM>();
        }

        protected override void SubComponent_Update()
        {
            base.SubComponent_Update();

            if (mFSM.CurrentState != SE2_FSM.EState.Idle
                && mFSM.CurrentState != SE2_FSM.EState.Runing)
            {
                return;
            }

            var direction = (Vector3?)null;

            if (Input.GetKey(KeyCode.A))
            {
                direction = -mCacheTransform.right;
            }
            else if (Input.GetKey(KeyCode.D))
            {
                direction = mCacheTransform.right;
            }

            if (direction != null)
            {
                mCacheTransform.position += -direction.Value * Model.moveSpeed * Time.deltaTime;
                mFSM.CurrentState = SE2_FSM.EState.Runing;

                if (!mCacheAnimator.GetCurrentAnimatorStateInfo(0).IsName(Model.moveAnimKey))
                    mCacheAnimator.Play(Model.moveAnimKey);
            }
        }
    }
}
